home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 July / CHIP Turkiye Temmuz 2000.iso / prog / share / 20 / 20.exe / COMPLEX.PAS < prev    next >
Pascal/Delphi Source File  |  2000-04-04  |  12KB  |  388 lines

  1. Unit Complex;
  2.  
  3. Interface
  4.  
  5.   Uses SysUtils, Math;
  6.  
  7.   Type
  8.     TComplex = record
  9.       real: Extended;
  10.       imag: Extended;
  11.     end;
  12.  
  13.   Function  MakeComplex(const real, imag: Extended): TComplex;
  14.   Procedure SetResult(var x,y: Extended; const complex: TComplex);
  15.  
  16.   Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z+A
  17.   Function  CAdd (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z+A
  18.   Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z-A
  19.   Function  CSub (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z-A
  20.   Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z*A
  21.   Function  CMul (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z*A
  22.   Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z/A
  23.   Function  CDiv (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z/A
  24.  
  25.   Procedure CSqrV(var Cmp1: TComplex);                           //  Z:=Z*Z
  26.   Function  CSqr (const Cmp1: TComplex): TComplex;               //  V:=Z*Z
  27.   Procedure CTripleV(var Cmp1: TComplex);                        //  Z:=Z*Z*Z
  28.   Function  CTriple (const Cmp1: TComplex): TComplex;            //  V:=Z*Z*Z
  29.   Procedure CFourV(var Cmp1: TComplex);                          //  Z:=Z*Z*Z*Z
  30.   Function  CFour (const Cmp1: TComplex): TComplex;              //  V:=Z*Z*Z*Z
  31.  
  32.   Procedure CFlipV(var Cmp1: TComplex);
  33.   Function  CFlip (const Cmp1: TComplex): TComplex;
  34.   Procedure CRevV (var Cmp1: TComplex);                          //  Z:=1/Z
  35.   Function  CRev  (const Cmp1: TComplex): TComplex;              //  V:=1/Z
  36.   Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex);    //  Z:=1/(Z-A)
  37.   Function  CRev2 (const Cmp1,Cmp2: TComplex): TComplex;         //  V:=1/(Z-A)
  38.  
  39.   Procedure CSqrtV(var Cmp1: TComplex);                          //  Z:=Sqrt(Z)
  40.   Function  CSqrt (const Cmp1: TComplex): TComplex;              //  V:=Sqrt(Z)
  41.   Procedure CExpV (var Cmp1: TComplex);                          //  Z:=Exp(Z)
  42.   Function  CExp  (const Cmp1: TComplex): TComplex;              //  V:=Exp(Z)
  43.   Procedure CLnV  (var Cmp1: TComplex);                          //  Z:=Ln(Z)
  44.   Function  CLn   (const Cmp1: TComplex): TComplex;              //  V:=Ln(Z)
  45.   Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex);         //  Z:=Z^A
  46.   Function  CPower (const Cmp1,Cmp2: TComplex): TComplex;        //  V:=Z^A
  47.  
  48.   Procedure CSinV (var Cmp1: TComplex);                          //  Z:=Sin(Z)
  49.   Function  CSin  (const Cmp1: TComplex): TComplex;              //  V:=Sin(Z)
  50.   Procedure CCosV (var Cmp1: TComplex);                          //  Z:=Cos(Z)
  51.   Function  CCos  (const Cmp1: TComplex): TComplex;              //  V:=Cos(Z)
  52.   Procedure CTanV (var Cmp1: TComplex);                          //  Z:=Tan(Z)
  53.   Function  CTan  (const Cmp1: TComplex): TComplex;              //  V:=Tan(Z)
  54.   Procedure CSinhV(var Cmp1: TComplex);                          //  Z:=Sinh(Z)
  55.   Function  CSinh (const Cmp1: TComplex): TComplex;              //  V:=Sinh(Z)
  56.   Procedure CCoshV(var Cmp1: TComplex);                          //  Z:=Cosh(Z)
  57.   Function  CCosh (const Cmp1: TComplex): TComplex;              //  V:=Cosh(Z)
  58.  
  59.  
  60. Implementation
  61.  
  62.   Const SmallTol: Extended = 1E-25;
  63.  
  64. Function  MakeComplex(const real, imag: Extended): TComplex;
  65. Begin
  66.   Result.real:=real;
  67.   Result.imag:=imag;
  68. End;
  69.  
  70. Procedure SetResult(var x,y: Extended; const complex: TComplex);
  71. Begin
  72.   x:=complex.real;
  73.   y:=complex.imag;
  74. End;
  75.  
  76. { ------------------------------------------------------------------- }
  77. Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex);
  78. Begin
  79.   Cmp1.real:=Cmp1.real+Cmp2.real;
  80.   Cmp1.imag:=Cmp1.imag+Cmp2.imag;
  81. End;
  82.  
  83. Function  CAdd (const Cmp1, Cmp2: TComplex): TComplex;
  84. Begin
  85.   Result.real:=Cmp1.real+Cmp2.real;
  86.   Result.imag:=Cmp1.imag+Cmp2.imag;
  87. End;
  88.  
  89.  
  90. Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex);
  91. Begin
  92.   Cmp1.real:=Cmp1.real-Cmp2.real;
  93.   Cmp1.imag:=Cmp1.imag-Cmp2.imag;
  94. End;
  95.  
  96. Function  CSub (const Cmp1, Cmp2: TComplex): TComplex;
  97. Begin
  98.   Result.real:=Cmp1.real-Cmp2.real;
  99.   Result.imag:=Cmp1.imag-Cmp2.imag;
  100. End;
  101.  
  102.  
  103. Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex);
  104.   var tmp: Extended;
  105. Begin
  106.   tmp      :=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
  107.   Cmp1.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
  108.   Cmp1.real:=tmp;
  109. End;
  110.  
  111. Function  CMul (const Cmp1, Cmp2: TComplex): TComplex;
  112. Begin
  113.   Result.real:=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
  114.   Result.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
  115. End;
  116.  
  117.  
  118. Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex);
  119.   var tmp1,tmp2: Extended;
  120. Begin
  121.   tmp1       := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
  122.   tmp2       :=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp1;
  123.   Cmp1.imag  :=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp1;
  124.   Cmp1.real  := tmp2;
  125. End;
  126.  
  127. Function  CDiv (const Cmp1, Cmp2: TComplex): TComplex;
  128.   var tmp: Extended;
  129. Begin
  130.   tmp        := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
  131.   Result.real:=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp;
  132.   Result.imag:=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp;
  133. End;
  134.  
  135. { ------------------------------------------------------------------- }
  136.  
  137. Procedure CSqrV(var Cmp1: TComplex);
  138.   var tmp: Extended;
  139. Begin
  140.   tmp      :=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
  141.   Cmp1.imag:= Cmp1.real*Cmp1.imag * 2;
  142.   Cmp1.real:=tmp;
  143. End;
  144.  
  145. Function  CSqr (const Cmp1: TComplex): TComplex;
  146. Begin
  147.   Result.real:=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
  148.   Result.imag:= Cmp1.real*Cmp1.imag * 2;
  149. End;
  150.  
  151.  
  152. Procedure CTripleV(var Cmp1: TComplex);
  153.   var tmp: Extended;
  154. Begin
  155.   tmp      :=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
  156.   Cmp1.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
  157.   Cmp1.real:=tmp;
  158. End;
  159.  
  160. Function  CTriple (const Cmp1: TComplex): TComplex;
  161. Begin
  162.   Result.real:=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
  163.   Result.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
  164. End;
  165.  
  166. Procedure CFourV(var Cmp1: TComplex);
  167.   var tmpR, tmpI: Extended;
  168. Begin
  169.   tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
  170.   tmpI:= Cmp1.real*Cmp1.imag * 2;
  171.  
  172.   Cmp1.real:=(tmpR-tmpI)*(tmpR+tmpI);
  173.   Cmp1.imag:= tmpR*tmpI * 2;
  174. End;
  175.  
  176. Function  CFour (const Cmp1: TComplex): TComplex;
  177.   var tmpR, tmpI: Extended;
  178. Begin
  179.   tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
  180.   tmpI:= Cmp1.real*Cmp1.imag * 2;
  181.  
  182.   Result.real:=(tmpR-tmpI)*(tmpR+tmpI);
  183.   Result.imag:= tmpR*tmpI * 2;
  184. End;
  185.  
  186. { ------------------------------------------------------------------- }
  187.  
  188. Procedure CFlipV(var Cmp1: TComplex);
  189.   var tmp: Extended;
  190. Begin
  191.   tmp:=Cmp1.real;
  192.   Cmp1.real:=Cmp1.imag;
  193.   Cmp1.imag:=tmp;
  194. End;
  195.  
  196. Function  CFlip (const Cmp1: TComplex): TComplex;
  197. Begin
  198.   Result.real:=Cmp1.imag;
  199.   Result.imag:=Cmp1.real;
  200. End;
  201.  
  202.  
  203. Procedure CRevV(var Cmp1: TComplex);
  204.   var tmp: Extended;
  205. Begin
  206.   tmp       := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
  207.   Cmp1.real := Cmp1.real/tmp;
  208.   Cmp1.imag :=-Cmp1.imag/tmp;
  209. End;
  210.  
  211. Function  CRev (const Cmp1: TComplex): TComplex;
  212.   var tmp: Extended;
  213. Begin
  214.   tmp        := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
  215.   Result.real:= Cmp1.real/tmp;
  216.   Result.imag:=-Cmp1.imag/tmp;
  217. End;
  218.  
  219.  
  220. Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex);
  221.   var tmp: Extended;
  222. Begin
  223.   tmp       := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
  224.                (Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
  225.   Cmp1.real := (Cmp1.real-Cmp2.real)/tmp;
  226.   Cmp1.imag := (Cmp2.imag-Cmp1.imag)/tmp;
  227. End;
  228.  
  229. Function  CRev2 (const Cmp1,Cmp2: TComplex): TComplex;
  230.   var tmp: Extended;
  231. Begin
  232.   tmp        := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
  233.                 (Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
  234.   Result.real:= (Cmp1.real-Cmp2.real)/tmp;
  235.   Result.imag:= (Cmp2.imag-Cmp1.imag)/tmp;
  236. End;
  237.  
  238. { ------------------------------------------------------------------- }
  239.  
  240. Procedure CSqrtV(var Cmp1: TComplex);                          //  Z:=Sqrt(Z)
  241.   var tmp,tmp2: Extended;
  242. Begin
  243.   tmp :=Sqrt(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag);
  244.   tmp2:=Sqrt((Cmp1.real+tmp)/2);
  245.   Cmp1.imag:=Sqrt((tmp-Cmp1.real)/2);
  246.   Cmp1.real:=tmp2;
  247. End;
  248.  
  249. Function  CSqrt (const Cmp1: TComplex): TComplex;              //  V:=Sqrt(Z)
  250.   var tmp: Extended;
  251. Begin
  252.   tmp :=Sqrt(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag);
  253.   Result.real:=Sqrt((Cmp1.real+tmp)/2);
  254.   Result.imag:=Sqrt((tmp-Cmp1.real)/2);
  255. End;
  256.  
  257.  
  258. Procedure CExpV (var Cmp1: TComplex);                          //  Z:=Exp(Z)
  259.   var tmp: Extended;
  260. Begin
  261.   tmp :=Exp(Cmp1.real);
  262.   Cmp1.real:=tmp*Cos(Cmp1.imag);
  263.   Cmp1.imag:=tmp*Sin(Cmp1.imag);
  264. End;
  265.  
  266. Function  CExp  (const Cmp1: TComplex): TComplex;              //  V:=Exp(Z)
  267.   var tmp: Extended;
  268. Begin
  269.   tmp :=Exp(Cmp1.real);
  270.   Result.real:=tmp*Cos(Cmp1.imag);
  271.   Result.imag:=tmp*Sin(Cmp1.imag);
  272. End;
  273.  
  274.  
  275. Procedure CLnV  (var Cmp1: TComplex);                          //  Z:=Ln(Z)
  276.   var tmp: Extended;
  277. Begin
  278.   tmp :=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
  279.   Cmp1.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
  280.   Cmp1.real:=tmp;
  281. End;
  282.  
  283. Function  CLn   (const Cmp1: TComplex): TComplex;              //  V:=Ln(Z)
  284. Begin
  285.   Result.real:=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
  286.   Result.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
  287. End;
  288.  
  289.  
  290. Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex);         //  Z:=Z^A
  291.   var t: TComplex;
  292. Begin
  293.   t:=CLn(Cmp1);
  294.   t:=CMul(Cmp1,Cmp2);
  295.   Cmp1:=CExp(t);
  296. End;
  297.  
  298. Function  CPower (const Cmp1,Cmp2: TComplex): TComplex;        //  V:=Z^A
  299.   var t: TComplex;
  300. Begin
  301.   t:=CLn(Cmp1);
  302.   t:=CMul(Cmp1,Cmp2);
  303.   Result:=CExp(t);
  304. End;
  305.  
  306.  
  307. { ------------------------------------------------------------------- }
  308. Procedure CSinV (var Cmp1: TComplex);                          //  Z:=Sin(Z)
  309.   var tmp1,tmp2,tmp3: Extended;
  310. Begin
  311.   tmp1:=Exp(Cmp1.imag)/2;
  312.   tmp2:=0.25/tmp1;
  313.   tmp3:=Sin(Cmp1.real)*(tmp1+tmp2);
  314.   Cmp1.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
  315.   Cmp1.real:=tmp3;
  316. End;
  317.  
  318. Function  CSin  (const Cmp1: TComplex): TComplex;              //  V:=Sin(Z)
  319.   var tmp1,tmp2: Extended;
  320. Begin
  321.   tmp1:=Exp(Cmp1.imag)/2;
  322.   tmp2:=0.25/tmp1;
  323.   Result.real:=Sin(Cmp1.real)*(tmp1+tmp2);
  324.   Result.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
  325. End;
  326.  
  327. Procedure CCosV (var Cmp1: TComplex);                          //  Z:=Cos(Z)
  328.   var tmp1: Extended;
  329. Begin
  330.   tmp1:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
  331.   Cmp1.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
  332.   Cmp1.real:=tmp1;
  333. End;
  334.  
  335. Function  CCos  (const Cmp1: TComplex): TComplex;              //  V:=Cos(Z)
  336. Begin
  337.   Result.real:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
  338.   Result.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
  339. End;
  340.  
  341. Procedure CTanV (var Cmp1: TComplex);                          //  Z:=Tan(Z)
  342.   var tmp: Extended;
  343. Begin
  344.   tmp      :=Sin(2*Cmp1.real)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
  345.   Cmp1.imag:=Sin(2*Cmp1.imag)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
  346.   Cmp1.real:=tmp;
  347. End;
  348.  
  349. Function  CTan  (const Cmp1: TComplex): TComplex;              //  V:=Tan(Z)
  350. Begin
  351.   Result.real:=Sin(2*Cmp1.real)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
  352.   Result.imag:=Sin(2*Cmp1.imag)/(Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag));
  353. End;
  354.  
  355.  
  356. Procedure CSinhV(var Cmp1: TComplex);                          //  Z:=Sinh(Z)
  357.   var tmp1: Extended;
  358. Begin
  359.   tmp1:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
  360.   Cmp1.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
  361.   Cmp1.real:=tmp1;
  362. End;
  363.  
  364. Function  CSinh (const Cmp1: TComplex): TComplex;              //  V:=Sinh(Z)
  365. Begin
  366.   Result.real:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
  367.   Result.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
  368. End;
  369.  
  370.  
  371. Procedure CCoshV(var Cmp1: TComplex);                          //  Z:=Cosh(Z)
  372.   var tmp1: Extended;
  373. Begin
  374.   tmp1:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
  375.   Cmp1.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
  376.   Cmp1.real:=tmp1;
  377. End;
  378.  
  379. Function  CCosh (const Cmp1: TComplex): TComplex;              //  V:=Cosh(Z)
  380. Begin
  381.   Result.real:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
  382.   Result.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
  383. End;
  384.  
  385.  
  386.  
  387. END.
  388.